from django.shortcuts import render
from  .models import *
import itertools
from datetime import datetime
from pyecharts.charts import Bar
from pyecharts import options as opts
# Create your views here.

from django.http import HttpResponse

def index(request):#进入注册界面c
    return render(request,'../templates/Warehouse/index.html')

def goods_in_out_return(request):#进入注册界面
    stock_out_list = StockOut.objects.all()
    stock_in_list = StockIn.objects.all()
    stock_return_list = ReturnInfo.objects.all()
    stock_return_list_new = StockOut.objects.none()
    for item in stock_out_list:
        if item.out_status == 0:
            item.out_status_cn = '未出库'
        else:
            item.out_status_cn = '已出库'
    for item in stock_in_list:
        if item.in_status == 0:
            item.in_status_cn = '未入库'
        else:
            item.in_status_cn = '已入库'
    for item in stock_return_list:
            res = OrderDetails.objects.filter(order_id=item.order.order_id)
            for thing in res:
                thing.order_id = item.order.order_id
                thing.goods_name = thing.goods.goods_name
                thing.return_time = item.process_time
                thing.return_status_cn = item.return_status
            stock_return_list_new = itertools.chain(stock_return_list_new,res)
    return render(request,'../templates/Warehouse/in_out_return.html', {'stock_out_list':stock_out_list,'stock_in_list':stock_in_list,'stock_return_list':stock_return_list_new})

def get_inv(request):
    get_inv_list = StockInfo.objects.all()
    if request.method == 'POST':
        for i in range(0,len(get_inv_list)):
            u = request.POST.get(str(i+1))
            if u:
                get_inv_list[i].stock_num_true = int(u)
                get_inv_list[i].stock_num_diff = get_inv_list[i].stock_num_true - get_inv_list[i].stock_num_system
                get_inv_list[i].save()
        return render(request,'../templates/Warehouse/pandian.html', {'stock_inv_list':get_inv_list})
    else:
        return render(request, '../templates/Warehouse/pandian.html', {'stock_inv_list': get_inv_list})

def change_stock(request):
    stock_list = StockInfo.objects.all()
    for item in stock_list:
        if item.stock_num_true:
            item.stock_num_system = item.stock_num_true
            item.stock_num_true = None
            item.stock_num_diff = None
            item.save()
    return render(request,'../templates/Warehouse/index.html')

def predict_num(request):
    goods_stock_list = StockInfo.objects.all()
    goods_order_list = OrderDetails.objects.all()
    predict_set = PredictNum.objects.all()
    predict_list=[]
    kucun_list=[]
    caigou_list=[]
    sell = 0
    flag = 0
    for i in range(0,len(goods_stock_list)):
        for item in goods_order_list:
            if item.goods.goods_id == goods_stock_list[i].goods.goods_id and flag<=20:
                sell += item.goods_num
                flag +=1
        predict_list.append(sell)
        kucun_list.append(3*sell)
        if 4*sell-goods_stock_list[i].stock_num_system > 0:
            caigou_list.append(4*sell-goods_stock_list[i].stock_num_system)
        else:
            caigou_list.append(0)
        sell = 0
        flag = 0
    for i in range(0,len(goods_stock_list)):
        goods_stock_list[i].predict_num=predict_list[i]
        goods_stock_list[i].kucun_num=kucun_list[i]
        goods_stock_list[i].caigou_num=caigou_list[i]
    if predict_set.exists():
        predict_set.delete()
    for item in goods_stock_list:
        predict_set.create(  goods_id = item.goods.goods_id,
                            goods_name = item.goods_name,
                            stock_num = item.stock_num_system,
                            predict_num = item.predict_num,
                            kucun_num = item.kucun_num,
                            caigou_num = item.caigou_num)
    return render(request,'../templates/Warehouse/predict_num.html',{'goods_list':goods_stock_list})

def req_plan(request):
    predict_set = PredictNum.objects.all()
    req_list = ReplenishRequest.objects.all()
    for item in predict_set:
        if item.caigou_num > 0:
            req_list.create(
                request_num= item.caigou_num,
                request_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
                procure_id = 'proc00'+datetime.now().strftime('%Y%m%d%H%M%S'),
                goods = StockInfo.objects.get(goods=GoodsInfo.objects.get(goods_id=item.goods_id))
            )
    return render(request,'../templates/Warehouse/index.html')

def update_stock(request):
    stock_out_list = StockOut.objects.filter(out_status = 0)
    stock_in_list = StockIn.objects.filter(in_status = 0)
    stock_return_list = ReturnInfo.objects.filter(return_status='已处理')
    stock_list = StockInfo.objects.all()
    for item in stock_list:
        out_num1 = 0
        in_num1 = 0
        return_num1 =0
        for out_item in stock_out_list:
            if out_item.goods.goods == item.goods:
                out_num1 += out_item.out_num
            stock_out_list.update(out_status=1)
        for in_item in stock_in_list:
            if in_item.goods_id == item.goods.goods_id:
                in_num1 += in_item.in_num
            stock_in_list.update(in_status=1)
        for return_order in stock_return_list:
            return_item_list = OrderDetails.objects.filter(order_id=return_order.order.order_id)
            for return_item in return_item_list:
                if return_item.goods == item.goods:
                    return_num1 += return_item.goods_num
            stock_return_list.update(return_status = '已退货入库')
        item.stock_num_system = item.stock_num_system - out_num1 + in_num1 + return_num1
        item.save()
    return render(request,'../templates/Warehouse/index.html')

def print_bar(request):
    stock_list = StockInfo.objects.all()
    y_list = []
    x_list = []
    for item in stock_list:
        x_list.append(item.goods_name)
        y_list.append(item.stock_num_system)
    bar = Bar(init_opts=opts.InitOpts(width="1500px", height="500px", page_title="img_view",))
    bar.add_xaxis(x_list)
    bar.add_yaxis('商品名称',y_list)
    bar.set_global_opts(title_opts=opts.TitleOpts(title="商品名称-系统库存量"))
    bar.render('../system/templates/WareHouse/print_bar.html')

    return render(request,'../templates/Warehouse/print_bar.html')